﻿// $Id$
/*! \file
 * \brief Definition of enumerations.
 *
 * \author $Author$
 * \date $Date$
 * \version $Revision$
 */

using System;
using Salamanca.DataAccess;
#if (!NET_CF)
using System.ComponentModel;
using System.Globalization;
using System.Resources;
#endif

namespace Northwind.DomainModel
{



#if (!NET_CF)
    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary>Specifies a description for a property or event that lies in the resources of the project.</summary>
    ///
    ///////////////////////////////////////////////////////////////////////////

    internal sealed class SRDescriptionAttribute:
        DescriptionAttribute
    {

        /// <summary>Initializes a new instance of the <see cref="SRDescriptionAttribute" /> class.</summary>
        /// <param name="name">The name of the string resource which contains the description of the property or of the event.</param>
        public SRDescriptionAttribute(string name):
            base(name)
        {
            _Name=name;
        }

        /// <summary>Gets the description stored in this attribute.</summary>
        public override string Description
        {
            get
            {
                if (!string.IsNullOrEmpty(_Name))
                {
                    try
                    {
                        string ret=Resources.SR.ResourceManager.GetString(_Name, CultureInfo.CurrentCulture);
                        if (!string.IsNullOrEmpty(ret))
                            return ret;
                    } catch (MissingManifestResourceException)
                    {
                    }
                }

                return base.Description;
            }
        }

        /// <summary>The name of the string resource which contains the description of the property or of the event.</summary>
        private string _Name;
    }



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary>Formats enumerations according to their description.</summary>
    ///
    ///////////////////////////////////////////////////////////////////////////

    public class DescriptionFormatter:
        ICustomFormatter,
        IFormatProvider
    {
        /// <summary>Constructor.</summary>
        public DescriptionFormatter()
        {
        }

        /// <summary>Gets an object that defines how to format the specified type.</summary>
        public object GetFormat(Type formatType)
        {
            if (formatType==typeof(ICustomFormatter))
                return this;

            return null;
        }

        /// <summary>Converts the value of a specified object to an equivalent string representation using specified format and culture-specific formatting information.</summary>
        public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            if ((arg!=null) && (arg is Enum) && ((format==null) || (format=="G")))
            {
                DescriptionAttribute[] da=(DescriptionAttribute[])(arg.GetType().GetField(arg.ToString())).GetCustomAttributes(typeof(DescriptionAttribute), false);
                foreach (DescriptionAttribute v in da)
                    return v.Description;
            }

            if (arg is IFormattable)
                return ((IFormattable)arg).ToString(format, formatProvider);
            else if (arg!=null)
                return arg.ToString();

            return string.Empty;
        }

        /// <summary>Gets an instance of this formatter.</summary>
        public static DescriptionFormatter Instance
        {
            get
            {
                if (_Instance==null)
                    _Instance=new DescriptionFormatter();
                return _Instance;
            }
        }

        /// <summary>An instance of this formatter.</summary>
        private static DescriptionFormatter _Instance;

    }
#endif

}
